package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"main1/models"
)

type UserController struct {
	beego.Controller
}

func (c *UserController) Roust() {

	username := c.GetString("username")
	password := c.GetString("password")
	user := models.User{

		Username: username,
		Password: password,
	}
	err := models.DB.Create(&user).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "注册失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {

		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "注册成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
func (c *UserController) Login() {
	username := c.GetString("username")
	password := c.GetString("password")

	var user models.User
	err := models.DB.Where("username=?", username).First(&user).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "账号输入错误",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		if user.Password == password {
			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "登录成功",
				Data: nil,
			}
			c.ServeJSON()
		} else {
			c.Data["json"] = JsonR{
				Code: 0,
				Msg:  "密码错误，登录失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		}

	}
}
